﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Security.Cryptography;

namespace DFGK_WMS.WebAPI.Controllers.权限管理
{
    /// <summary>
    /// 按钮
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ActionController : ControllerBase
    {
        public ActionController(IActionRepository actionRepository)
        {
            ActionRepository = actionRepository;
        }

        public IActionRepository ActionRepository { get; }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddAction(TAction m)
        {
            ApiResult apiResult = new ApiResult();
            bool IsOK = ActionRepository.AddAction(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "添加成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "添加失败！已存在该操作编码！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 显示按钮列表
        /// </summary>
        /// <param name="aname"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetActionList(string? aname = "", int status = 2, int pageIndex = 1, int pageSize = 10)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            ApiResult apiResult = new ApiResult();
            int totalCount;
            var list = ActionRepository.GetActionList(aname, status, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功！";
                apiResult.data = list;
                apiResult.count = totalCount;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败！";
            return Ok(apiResult);
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAction(int aid)
        {
            if(aid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = ActionRepository.DelAction(aid);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "删除成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "删除失败！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 根据主键获取信息
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ActionById(int aid)
        {
            if(aid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            var obj = ActionRepository.ActionById(aid);
            if (obj != null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功！";
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "按钮Id有误！";
            return Ok(apiResult);
        }

        /// <summary>
        /// 修改按钮
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdAction(TAction m)
        {
            if(m.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = ActionRepository.UpdAction(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "修改成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "修改失败！已存在该操作编码！";
            apiResult.data = false;
            return Ok(apiResult);
        }
    }
}
